package com.ylr.user.framework.resolver;

import com.ylr.user.framework.feign.client.YuUserClient;
import com.ylr.base.common.dto.PageDto;
import com.ylr.user.common.dto.user.UserDto;
import com.ylr.base.common.exception.YlrException;
import com.ylr.base.common.tool.Result;
import com.ylr.user.common.vo.user.UserFormVo;
import com.ylr.user.common.vo.user.UserVo;
import com.ylr.base.common.dto.NameValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-12 14:54:17
 * className: YuUserResolver
 * version: 1.0
 * description:
 */
@Component
public class YuUserResolver {

    private static final Logger log = LoggerFactory.getLogger(YuUserResolver.class);

    @Value("${spring.application.simple-name:ylr}")
    private String applicationSimpleName;

    private final YuUserClient yuUserClient;

    public YuUserResolver(YuUserClient yuUserClient) {
        this.yuUserClient = yuUserClient;
    }

    /**
     * 保存用户操作
     * @param userVo 参数对象
     * @return 用户主键
     */
    public Long saveUser(UserVo userVo) {
        Result<Long> result = yuUserClient.saveUser(userVo);
        if (result.isFail()) {
            log.warn("{}模块 => 保存用户操作失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 分页查询用户操作
     * @param userFormVo 参数对象
     * @return 用户分页数据
     */
    public PageDto<UserDto> pageUserList(UserFormVo userFormVo) {
        Result<PageDto<UserDto>> result = yuUserClient.pageUserList(userFormVo);
        if (result.isFail()) {
            log.warn("{}模块 => 分页查询用户操作失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 获取用户操作
     * @param id 用户主键
     * @return 用户对象
     */
    public UserDto getUser(Long id) {
        Result<UserDto> result = yuUserClient.getUser(id);
        if (result.isFail()) {
            log.warn("{}模块 => 获取用户操作失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 批量获取用户操作
     * @param userIdList 用户主键列表
     * @return 用户列表
     */
    public List<UserDto> getUserList(List<Long> userIdList) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return Collections.emptyList();
        }
        Result<List<UserDto>> result = yuUserClient.getUserList(userIdList);
        if (result.isFail()) {
            log.warn("{}模块 => 批量获取用户操作失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 获取用户名字映射操作
     * 根据用户主键集合，查询用户名称并进行映射
     * @param userIdSet 用户主键集合
     * @return 用户名字映射
     */
    public Map<Long, String> getUsernameMap(Set<Long> userIdSet) {
        if (CollectionUtils.isEmpty(userIdSet)) {
            return Collections.emptyMap();
        }
        Result<Map<Long, String>> result = yuUserClient.getUsernameMap(userIdSet);
        if (result.isFail()) {
            log.warn("{}模块 => 获取用户名字映射操作。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 获取用户名字列表操作
     * @param userIdSet  用户主键集合
     * @return 用户名字列表
     */
    public List<NameValue<String>> getUsernameList(Set<Long> userIdSet) {
        if (CollectionUtils.isEmpty(userIdSet)) {
            return Collections.emptyList();
        }
        Result<List<NameValue<String>>> result = yuUserClient.getUsernameList(userIdSet);
        if (result.isFail()) {
            log.warn("{}模块 => 获取用户名字列表操作。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 修改用户操作
     * @param userVo 参数对象
     */
    public void updateUser(UserVo userVo) {
        Result<Object> result = yuUserClient.updateUserBase(userVo);
        if (result.isFail()) {
            log.warn("{}模块 => 修改用户操作失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }

    }

    /**
     * 删除用户操作
     * @param idList 主键列表
     * @return 成功删除用户个数
     */
    public Integer deleteUser(List<Long> idList) {
        Result<Integer> result = yuUserClient.deleteUser(idList);
        if (result.isFail()) {
            log.warn("{}模块 => 删除用户操作失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

}
